package cn.marinda.resturants.controller;


import cn.marinda.resturants.entity.Activity;
import cn.marinda.resturants.entity.Administrator;
import cn.marinda.resturants.enums.EntityJson;
import cn.marinda.resturants.global.Pages;
import cn.marinda.resturants.service.AdministratorService;
import cn.marinda.resturants.utils.ApiResult;
import cn.marinda.resturants.utils.IOUtils;
import cn.marinda.resturants.utils.PagesResult;
import cn.marinda.resturants.utils.SpringBeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/** 管理员控制器
 * @date 2021/12/2
 * @author Marinda
 * @version 1.0
 *
 */
@RestController
@RequestMapping("/administrator")
public class AdministratorController {

    @Resource
    private AdministratorService administratorService;

    /**
     * 查询所有Administrator
     *
     * @return 所有Administrator
     */
    @RequestMapping("/queryList")
    public ApiResult queryList() {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,administratorService.queryList());
    }

    /**
     * 添加一个管理员信息
     *
     * @param administrator 管理员信息
     * @return 结果
     */
    @RequestMapping("/add")
    public ApiResult addAdministrator(Administrator administrator) {
        return administratorService.addAdministrator(administrator) ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) : new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);
    }

    /** 通过uid来查询一个用户
     *
     *
     * @param userUid 用户uid
     * @return 用户
     */
    @RequestMapping("/index")
    private ApiResult selectUserUid(@RequestParam("userUid") String userUid){
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE, Arrays.asList(administratorService.selectAdministratorid(userUid)));

    }
    /** 列表分页
     *
     * @date 2021/12/1
     * @return 结果
     */
    @RequestMapping("/pages")
    public PagesResult selectPages(@RequestParam("page") int page, @RequestParam("limit") int limit)
    {
        IPage<Administrator> pages = administratorService.selectAdministratorPages(new Pages<Administrator>(page,limit));
        return new PagesResult(0,pages.getRecords(),(int)pages.getTotal(),pages.getSize());
    }

    /**
     * 删除一个管理员信息
     *
     * @param userUid 管理员
     * @return 结果
     */
    @RequestMapping("/delete")
    public ApiResult deleteAdministrator(String userUid) {
        return administratorService.deleteAdministrator(userUid) ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) : new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);
    }

    /**
     * 查询一个身份类型下的所有管理员
     *
     * @param type 类型
     * @return 包含administrator 的一个集合
     */
    @RequestMapping("/typeAdmin")
    public ApiResult selectTypeAdmin(String type) {
        return administratorService.selectTypeAdmin(type)!= null ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) : new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);

    }

    /**
     * 通过查询管理员名字找到这个Admin
     *
     * @param name adminName
     * @return Administrator
     */
    @RequestMapping("/adminName")
    public ApiResult selectAdminName(String name) {
        return administratorService.selectAdminName(name)!= null ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) : new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);

    }

    /**
     * 通过查询Administrator的Phone能找到这个Administator
     *
     * @param phone admin phone
     * @return Administrator
     */
    @RequestMapping("/phone")
    public ApiResult selectPhone(String phone) {
        return administratorService.selectPhone(phone)!= null ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) : new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);

    }

    /**
     * 乐观锁修改管理员细腻些
     *
     * @param administrator 管理员
     * @return 结果
     */
    @RequestMapping("/update")
    public ApiResult updateAdministrator(Administrator administrator) {
        return administratorService.updateAdministrator(administrator) ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) : new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);

    }

}
